<?php
function download_url($url) {
    ob_start();
    readfile($url);
    return ob_get_clean();
}

function get_3($url) {
    print "Entering: $url\n";
    $categories = array();
    $content = download_url($url);
    if (preg_match("/<ul class=\'cat cat-sub\'>(.*)<\/div>/Uis", $content, $matches)) {
        if (preg_match_all("/<a href=\'([^\']*)\' onclick=\"[^\"]*\">(.*)<\/a>/siU", $matches[1], $matches)) {
            foreach ($matches[1] as $k=>$v) {
                $url = $v;
                preg_match("([^(.*)]*)",$matches[2][$k],$name);
                $name = trim($name[0]);
                $categories[$name] = $url;
            }
        }
    }
    return $categories;
}

function get_2($url) {
    print "Entering: $url\n";
    $categories = array();
    $content = download_url($url);
    if (preg_match_all("/<a HREF=\"([^\"]*)\" class=\"M11B\">(.*)<\/a>/siU", $content, $matches)) {
        foreach ($matches[1] as $k=>$v) {
            $url = $v;
            preg_match("([^(.*)]*)",$matches[2][$k],$name);
            $name = $name[1];
            $name = trim($matches[2][$k]);
            $categories[$name] = $url;
        }
        return $categories;
    }
}


function get_1($url) {
    print "Entering: $url\n";
    $categories = array();
    $content = download_url($url);
    if (preg_match("/<ul class=\"cat\">(.*)<\/div>/Uis", $content, $matches)) {
        if (preg_match_all("/<a\shref=\"([^\"]*)\">(.*)<\/a>/siU", $matches[1], $matches)) {
            foreach ($matches[1] as $k=>$v) {
                $url = $v;
                preg_match("([^(.*)]*)",$matches[2][$k],$name);
                $name = $name[1];
                $name = trim($matches[2][$k]);
                $categories[$name] = $url;
            }
            return $categories;
        }
    }
}

function recursive_sub_categories(&$sub_categories) {
    global $base_url;
    foreach($sub_categories as $sub=>$url) {
        print "*";
        $subs = get_3($base_url.$url);
        #print_r($subs);
        if (count($subs) > 0) {
            print "-";
            recursive_sub_categories($subs);
            $sub_categories[$sub] = $subs;
            #break;
        }
    }
}

#$base_url = "http://www.tradera.com/PS3-c3_302557";
#$categories = get_3($base_url);
#recursive_sub_categories($categories);
#print_r($subc);


$base_url = "http://www.tradera.com";
$categories = get_1($base_url);
foreach($categories as $category=>$url) {
    print "+";
    #if ($url == "/TV-spel-Dataspel-30") {
    $sub_categories = get_2($base_url.$url);
    recursive_sub_categories($sub_categories);
    $categories[$category] = $sub_categories;
    #break;
    #}
}
print "\n";

print_r($categories);

system("echo '' > categories.mig");
system("echo \"execute \\\"INSERT INTO categories (name,category_id,id) VALUES ('Annonser','0','10')\\\"\" >> categories.mig");

function new_id() {
    global $id;
    $id++;
    return $id;
}

function add_category($parent,$category) {
    if (is_array($category))
    foreach ($category as $c=>$v) {
        if (empty($c))
        break;
        $this_id = new_id();
        system("echo \"execute \\\"INSERT INTO categories (name,category_id,id) VALUES ('$c','$parent','$this_id')\\\"\" >> categories.mig");
        add_category($this_id,$v);
    }
}

$id = 10;
add_category("$id",$categories);
?>
